// pages/doctor-my-data-rz/doctor-my-data-rz.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorId: '',
    imgUrl: app.globalData.imgUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadings();
  },

  //获取医生个人信息
  loadings: function() {
    var doctorId = wx.getStorageSync('userid');
    var that = this;
    app.request({
      url: 'doctor/doctor/doctorCardInfo',
      data: {
        'doctorId': doctorId
      },
      success(res) {
        wx.hideLoading();
        that.setData({
          picture: res.data[0],
          cardFront: res.data[0].cardFront,
          cardOpposite: res.data[0].cardOpposite,
          //用来以后对比路径是否对
          cardFronts: res.data[0].cardFront,
          cardOpposites: res.data[0].cardOpposite,
        })
      }
    })
  },
  //保存
  nextstep: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '修改身份证信息需要重新审核！',
      success(res) {
        if (res.confirm) {
          that.present();
        } else if (res.cancel) {
        }
      }
    })
  },
  //编辑身份证
  present: function() {
    var that = this;
    var cardFront = that.data.cardFront;
    var cardOpposite = that.data.cardOpposite;
    var cardFronts = that.data.cardFronts;
    var cardOpposites = that.data.cardOpposites;
    if (String(cardFront) == String(cardFronts)) {
      wx.showToast({
        title: '请更改身份证正面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (String(cardOpposite) == String(cardOpposites)) {
      wx.showToast({
        title: '请更改身份证背面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    app.request({
      url: 'doctor/doctor/editCardInfo',
      data: {
        'cardFront': cardFront,
        'cardOpposite': cardOpposite,
      },
      success(res) {
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 1000
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/doctor-login/doctor-login',
          })
        }, 2000)
      }
    })
  },
  /**
   * loading加载
   */
  showLoading: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  },
  // 身份证正面上传图片
  cardFront: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function(res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardFront: path
            })
            // that.present();
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 身份证背面上传图片
  cardOpposite: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function(res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardOpposite: path
            })
           
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  }
})